//
//  HNMyViewController.m
//  JHFBaseProject
//
//  Created by 朱传正 on 2016/12/29.
//  Copyright © 2016年 好牛. All rights reserved.
//

#import "HNMyViewController.h"
//#import "HNLoginViewController.h"
#import "HNSettingViewController.h"
#import "HNMyEarningsController.h"
#import "HNFeeRecordController.h"
#import "HNMyOrderController.h"
#import "HNAboutOurController.h"
#import "HNMyIntegralController.h"




@interface HNMyViewController ()

@property (nonatomic,strong)NSDictionary *userInfo;

@property (nonatomic,assign)CGFloat contentHeight;

@end

@implementation HNMyViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    [self updateUserInfo];
}


-(void)updateUserInfo{
    NSDictionary *userInfo = [AppConfig getUserInfo];
    NSLog(@"%@",userInfo);
    
    NSString *imageStr = [NSString stringWithFormat:@"%@%@",url_Base,[userInfo objectForKey:@"photo"]];
    [_headImage sd_setImageWithURL:[NSURL URLWithString:imageStr] placeholderImage:[UIImage imageNamed:@"D99GN4P63Q4F.png"]];
    
    _headTel.text = [NSString stringWithFormat:@"%@",[userInfo objectForKey:@"hnLoginName"]];
    
    _headName.text = [NSString stringWithFormat:@"%@",[userInfo objectForKey:@"nickname"]];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}



- (IBAction)settingAction:(UIButton *)sender {
    HNSettingViewController *settingVC = [[ HNSettingViewController alloc]init];
    [self.navigationController pushViewController:settingVC animated:YES];
    
}




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}


-(void)drawView{
    
    
    _headImage.layer.masksToBounds = YES;
    _headImage.layer.cornerRadius = _headImage.frame.size.height/2;
    
    _contentHeight = 180;
    
    _cellView.backgroundColor = RGBJ(241);

    
    NSArray *imageArr = [NSArray arrayWithObjects:@"¥.png",@"fee_record.png",@"zhengjin.png",@"白.png",@"552cc3db89e7e_128.png",@"图层-39.png", nil];
    NSArray *nameArr = [NSArray arrayWithObjects:@"我的收入",@"诊金记录",@"我的订单",@"关于我们",@"积分兑换",@"全国服务热线：888-88888888", nil];
    for (int i = 0; i<imageArr.count; i++) {
        UIView *itemView = [[UIView alloc]initWithFrame:CGRectMake(0, _contentHeight, kScreen_Width, 50)];
        itemView.backgroundColor = [UIColor whiteColor];
        [_cellView addSubview:itemView];
        
        _contentHeight = _contentHeight + itemView.frame.size.height;
        
        UIImageView *signImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 30, 30)];
        signImageView.image = [UIImage imageNamed:imageArr[i]];
        [itemView addSubview:signImageView];
        
        //标题
        UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(60,15,kScreen_Width- 100,20)];
        nameLabel.numberOfLines = 1;
        nameLabel.clipsToBounds = YES;
        nameLabel.textAlignment = NSTextAlignmentLeft;
        nameLabel.font = [UIFont systemFontOfSize:13];
        
        nameLabel.textColor = RGBJ(10);
        nameLabel.text = nameArr[i];
        [itemView addSubview:nameLabel];
        
        if (i != nameArr.count - 1) {
            UIImageView *rightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(kScreen_Width - 50, 15, 20, 20)];
            rightImageView.image = [UIImage imageNamed:@"rightBtn.png"];
            [itemView addSubview:rightImageView];
            
            UILabel *lineLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(0,itemView.frame.size.height-1, kScreen_Width, 0.5)];
            lineLabel2.backgroundColor = [UIColor colorWithHexString:@"cecece"];
            [itemView addSubview:lineLabel2];
            [itemView bringSubviewToFront:lineLabel2];
       
        }
        //添加点击按钮——覆盖在原来的View上
        UIButton *clickBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [clickBtn setTitle:@"" forState:UIControlStateNormal];
        [clickBtn setBackgroundColor:RGBA(255, 255, 255, 0)];
        clickBtn.tag = 100 + i;
        [clickBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        clickBtn.frame = itemView.frame;//大小和位置
        [clickBtn addTarget:self action:@selector(enterOtherVC:) forControlEvents:UIControlEventTouchUpInside];
        [_cellView addSubview:clickBtn];
        
    }
    
    
    
    _cellView.contentSize = CGSizeMake(kScreen_Width, _contentHeight);
}


-(void)initData{
    [self getUserInfo];
}

-(void)enterOtherVC:(UIButton *)btn{
    NSInteger tag = btn.tag - 100;
    if (tag == 0) {
        HNMyEarningsController *normalMsgVC = [[HNMyEarningsController alloc]init];
        [self.navigationController pushViewController:normalMsgVC animated:YES];
    }else if (tag == 1){
        HNFeeRecordController *normalMsgVC = [[HNFeeRecordController alloc]init];
        [self.navigationController pushViewController:normalMsgVC animated:YES];
    }else if (tag == 2){
        HNMyOrderController *normalMsgVC = [[HNMyOrderController alloc]init];
        [self.navigationController pushViewController:normalMsgVC animated:YES];
    }else if (tag == 3){
        HNAboutOurController *normalMsgVC = [[HNAboutOurController alloc]init];
        [self.navigationController pushViewController:normalMsgVC animated:YES];
    }else if (tag == 4){
        HNMyIntegralController *normalMsgVC = [[HNMyIntegralController alloc]init];
        [self.navigationController pushViewController:normalMsgVC animated:YES];
    }else if (tag == 5){
        NSString *phoneNumberURLString = [@"tel://" stringByAppendingString:@"888-88888888"];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumberURLString]];
    }
}



-(void)getUserInfo{
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithObjectsAndKeys:static_sessionId,@"sessionId",nil];
    
    NSLog(@"param = %@",param);
    
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager manager] initWithBaseURL:[NSURL URLWithString:url_Base]];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url_userInfo_detail parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError * error;
        NSLog(@"responseObject = %@",responseObject);
        
        NSDictionary * data=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
        [self hideHud];
        if (error) {
            NSLog(@"====error1:%@",error);
            [self showHint:@"获取用户信息失败"];
            NSLog(@"==dict:%@",data);
            return;
        }
        NSLog(@"==dict:%@",data);
        
        NSString * result = [NSString stringWithFormat:@"%@",[data objectForKey:@"result"]];
        if ([result isEqualToString:@"1"]) {
            _userInfo = [NSDictionary dictionaryWithDictionary:[data objectForKey:@"obj"]];
            NSLog(@"------obj = %@",_userInfo);
            
            [Util addUserId:[NSString stringWithFormat:@"%@",[_userInfo objectForKey:@"userId"]]];
            NSLog(@"fsdkgj %@",[Util getUserId]);
            
            NSString *realName = [NSString stringWithFormat:@"%@",[_userInfo objectForKey:@"realName"]];
            
            NSString *userImageStr = [NSString stringWithFormat:@"%@",[_userInfo objectForKey:@"photo"]];
            if ([AppConfig isNULL:userImageStr]) {
                userImageStr = @"null";
            }
            NSString *userNickname = [NSString stringWithFormat:@"%@",[_userInfo objectForKey:@"nickname"]];
            if ([AppConfig isNULL:userNickname]) {
                
                userNickname = realName;
                
            }
            NSString *userUserName = [NSString stringWithFormat:@"%@",[_userInfo objectForKey:@"username"]];
            if ([AppConfig isNULL:userUserName]) {
                userImageStr = @"未设置";
            }
            NSDictionary *saveDic = [NSDictionary dictionaryWithObjectsAndKeys:userImageStr,@"photo",userUserName,@"hnLoginName",userNickname,@"nickname", nil];
            
            [AppConfig saveUserInfo:saveDic];
            
            NSString *imageStr = [NSString stringWithFormat:@"%@%@",url_Base,userImageStr];
            [_headImage sd_setImageWithURL:[NSURL URLWithString:imageStr] placeholderImage:[UIImage imageNamed:@"D99GN4P63Q4F.png"]];
            
            _headTel.text = userUserName;
            
            _headName.text = userNickname;
            
        }else{
            NSString * info = [NSString stringWithFormat:@"%@",[data objectForKey:@"msg"]];
            if ([info isEqualToString:msg_InvalidLoginInformation]) {
                
                [AppConfig removeUserInfo];
                
                [[EMClient sharedClient] logout:YES];
                [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO];
                
                HNLoginViewController *loginVC = [[HNLoginViewController alloc]init];
                
                [self.navigationController pushViewController:loginVC animated:YES];
            }
            [self showHint:info];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull  error) {
        NSLog(@"====error:%@",error);
        [self hideHud];
        [self showHint:@"获取用户信息失败"];
    }];
    
    
}



@end
